//
//  TKIMMulImageBrowserView.m
//  TKPhotoSelectDemo
//
//  Created by liupm on 16/4/21.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMMulImageBrowserView.h"
#import "TKMMulBrowserCell.h"
#import "TKIMImageFooterView.H"
//纵向间距
#define vSpace 0
//横向间距
#define hSpace 0

@interface TKIMMulImageBrowserView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate>
@property(nonatomic,strong) UICollectionView *collectionView;
//数据源<TKIMPhotoModel>
@property(nonatomic,strong) NSMutableArray *dataSource;
//底部视图
@property(nonatomic,strong) TKIMImageFooterView *footerView;

@end
@implementation TKIMMulImageBrowserView
static NSString * const reuseIdentifier = @"Cell";

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataSource = [NSMutableArray array];
        [self drawView];
    }
    return self;
}


-(void)drawView
{
    [self addSubview:self.collectionView];
    [self addSubview:self.footerView];
}


/**
 *  @Author lpm, 16-04-21 10:04:43
 *
 *  @brief  刷新数据
 *
 *  @param dataSource
 */
-(void)reloadData:(NSMutableArray *)dataSource
{
    if (dataSource)
    {
        _dataSource = dataSource;
        [self.collectionView reloadData];
    }
}

#pragma mark <UICollectionViewDataSource>
/**
 *  @Author lpm, 16-04-20 19:04:00
 *
 *  @brief
 *
 *  @param collectionView
 *
 *  @return
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


/**
 *  @Author lpm, 16-04-20 19:04:09
 *
 *  @brief
 *
 *  @param collectionView
 *  @param section
 *
 *  @return
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}


/**
 *  @Author lpm, 16-04-20 19:04:23
 *
 *  @brief
 *
 *  @param collectionView
 *  @param indexPath
 *
 *  @return
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TKMMulBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    TKIMPhotoModel *model = _dataSource[indexPath.row];
    cell.model = model;
    return cell;
}



/**
 *  @Author lpm, 16-04-21 09:04:42
 *
 *  @brief  行被选中
 *
 *  @param collectionView
 *  @param indexPath
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSLog(@"didSelectItemAtIndexPath");
}


/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  滚动结束委托
 *
 *  @param scrollView
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger index = offsetX/self.frame.size.width;
    if (index>=0) {
        if (_delegate &&[_delegate respondsToSelector:@selector(didScrollToPage:)])
        {
            [_delegate didScrollToPage:index+1];
        }
    }
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate)
    {
        [self scrollViewDidEndDecelerating:scrollView];
    }
}

/**
 *  @Author lpm, 16-04-21 13:04:24
 *
 *  @brief  事件路由
 *
 *  @param eventName
 *  @param userInfo
 */
-(void)routerEventWithName:(NSString *)eventName userInfo:(NSObject *)userInfo
{
    if([eventName isEqualToString:TKIMImageBrowersHideNavBarEvent])
    {
        BOOL hide = self.footerView.isHidden;
        if (hide)
        {
            hide = NO;
        }
        else
        {
            hide = YES;
        }
        [UIView animateWithDuration:0.25 animations:^{
            self.footerView.hidden = hide;
        }];
        [super routerEventWithName:TKIMImageBrowersHideNavBarEvent userInfo:@(hide)];
    }
    else
    {
        [super routerEventWithName:eventName userInfo:userInfo];
    }
}

#pragma mark 属性

-(UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 5, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate =self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[TKMMulBrowserCell class] forCellWithReuseIdentifier:reuseIdentifier];
        _collectionView.userInteractionEnabled = YES;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
    }
    return _collectionView;
}


-(UIView *)footerView
{
    if (!_footerView)
    {
        _footerView = [[TKIMImageFooterView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-44, self.frame.size.width, 44)];
        _footerView.backgroundColor = [UIColor whiteColor];
        _footerView.alpha = 0.95;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _footerView.frame.size.width, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [_footerView addSubview:line];
        _footerView.previewButton.hidden = YES;
        _footerView.confirmButton.enabled = YES;
    }
    return _footerView;
}

@end
